from models.models.RequestResult import RequestResult as Res
from django.views.decorators.csrf import csrf_exempt
from django.core import serializers
from models.models.SystemMenu import SystemMenu
from django.http.response import JsonResponse
from django.shortcuts import render
import json
import math


# 获取树状菜单列表
def get_menu_tree(request):
    menus = SystemMenu.objects.filter(parent_id=0)
    res_menus = []
    for menu in menus:
        res_menu = dict()
        res_menu["id"] = menu.id
        res_menu["menu_name"] = menu.menu_name
        res_menu["menu_url"] = menu.menu_url
        res_menu["menu_type"] = menu.menu_type
        res_menu["parent_id"] = menu.parent_id
        child_menus = SystemMenu.objects.filter(parent_id=menu.id)
        res_child_menus = []
        for child_menu in child_menus:
            res_child_menu = dict()
            res_child_menu["id"] = child_menu.id
            res_child_menu["menu_name"] = child_menu.menu_name
            res_child_menu["menu_url"] = child_menu.menu_url
            res_child_menu["menu_type"] = child_menu.menu_type
            res_child_menu["parent_id"] = child_menu.parent_id
            res_child_menus.append(res_child_menu)
        res_menu["child_menus"] = res_child_menus
        res_menus.append(res_menu)
    return JsonResponse(Res.success(res_menus))


# 获取菜单列表
def get_menu_list(request):
    page_number = request.GET.get("page_number")
    if page_number is None:
        page_number = 1
    else:
        page_number = int(page_number)
    page_size = 10
    limit = (page_number - 1) * page_size
    offset = limit+page_size
    count = 0
    menus = []
    menu_name = request.GET.get("menu_name")
    if not menu_name:
        menus = SystemMenu.objects.filter().order_by("-id")[limit:offset]
        count = SystemMenu.objects.filter().count()
    else:
        menus = SystemMenu.objects.filter(menu_name__contains=menu_name).order_by("-id")[limit:offset]
        count = SystemMenu.objects.filter(menu_name__contains=menu_name).count()
    page_total = math.ceil(count / page_size)
    res_menus = []
    for menu in menus:
        res_menu = dict()
        res_menu["id"] = menu.id
        res_menu["menu_name"] = menu.menu_name
        res_menu["menu_url"] = menu.menu_url
        if menu.menu_type == "1":
            res_menu["menu_type"] = "目录"
        else:
            res_menu["menu_type"] = "菜单"
        if menu.parent_id == 0:
            res_menu["parent_id"] = "主类目"
        else:
            parent_menu = SystemMenu.objects.filter(id=menu.parent_id).first()
            res_menu["parent_id"] = parent_menu.menu_name
        res_menus.append(res_menu)
    res_date = dict()
    res_date["list"] = res_menus
    res_date["page_number"] = page_number
    res_date["page_total"] = page_total
    return JsonResponse(Res.success(res_date))


# 转发到系统菜单管理页
def to_menu(request):
    fields = SystemMenu.get_fields()
    menus = SystemMenu.objects.filter(menu_type='1')
    res_menus = []
    for menu in menus:
        res_menu = dict()
        res_menu['value'] = menu.id
        res_menu['label'] = menu.menu_name
        res_menus.append(res_menu)
    res = {
        "name": "菜单",
        "fields": fields,
        "selectValues": json.dumps({
            "parent_id_values": str(res_menus),
            "menu_type_values": str([{"value": 1, "label": "目录"}, {"value": 2, "label": "菜单"}])
        }),
        "getListUrl": "/admin/menu/getList/",
        "getOneUrl": "/admin/menu/getOne/",
        "insertUrl": "/admin/menu/insert/",
        "updateUrl": "/admin/menu/update/",
        "deleteUrl": "/admin/menu/delete/"
    }
    return render(request, "storeAdmin/common/list.html", res)


# 获取某一菜单
def get_menu_one(request):
    id = request.GET.get('id')
    menu = SystemMenu.objects.filter(id=id).first()
    return JsonResponse(menu.to_dict())


# 新增菜单
@csrf_exempt
def insert(request):
    menu = SystemMenu()
    menu.menu_name = request.POST.get("menu_name")
    menu.menu_url = request.POST.get("menu_url")
    menu.menu_type = request.POST.get("menu_type")
    menu.parent_id = request.POST.get("parent_id")
    menu.save()
    return JsonResponse(Res.success(None))


# 更新菜单
@csrf_exempt
def update(request):
    menu = SystemMenu.objects.get(id=request.POST.get("id"))
    menu.menu_name = request.POST.get("menu_name")
    menu.menu_url = request.POST.get("menu_url")
    menu.menu_type = request.POST.get("menu_type")
    menu.parent_id = request.POST.get("parent_id")
    menu.save()
    return JsonResponse(Res.success(None))


# 删除菜单
def delete(request):
    id = request.GET.get("id")
    menu = SystemMenu.objects.filter(id=id).first()
    menu.delete()
    return JsonResponse(Res.success(None))
